package services

import (
	"beergo/app/check/documents"
	"beergo/app/check/models"
	"beergo/lib/tool"
)

var rep documents.GeoRepository

func init() {
	rep = documents.GeoRepository{}
}

func UpdateOrInsertService( service models.Service) {
	find:=rep.FindOneBy(tool.M{"src":"Service","srcId":service.Id},nil)
	if find.Id.IsZero() {
		find = documents.Geo{}
		find.Src = "Service"
		find.SrcId = int64(service.Id)
		//find.Loc = new(documents.Loc)
	}
	//find.Loc.Lat = service.Lat
	//find.Loc.Lng = service.Lng
	find.Loc = []float64{service.Lng,service.Lat}
	//find.SrcData = service.Scan("",5,"")
	find.SrcData = service.ToArray("")
	find.SrcData["id"] = service.Id
	e:=find.Save()
	if e != nil {println("服务位置更新时发生错误",e.Error())}
}

func UpdateOrInsertTask( task models.Task) {
	find:=rep.FindOneBy(tool.M{"src":"Task","srcId":task.Id},nil)
	if find.Id.IsZero() {
		find = documents.Geo{}
		find.Src = "Task"
		find.SrcId = int64(task.Id)
		//find.Loc = new(documents.Loc)
	}
	//find.Loc.Lat = task.Lat
	//find.Loc.Lng = task.Lng
	find.Loc = []float64{task.Lng,task.Lat}
	//做好冗余数据
	//find.SrcData = task.Scan("",5,"")
	find.SrcData = task.ToArray("")
	find.SrcData["id"] = task.Id


	e:=find.Save()
	if e != nil {println("任务位置更新时发生错误",e.Error())}
}


func UpdateOrInsertOrg( task models.Org) {
	if task.Status.Id < 2{
		return
	}
	find:=rep.FindOneBy(tool.M{"src":"Org","srcId":task.Id},nil)
	if find.Id.IsZero() {
		find = documents.Geo{}
		find.Src = "Org"
		find.SrcId = int64(task.Id)
	}
	find.Loc = []float64{task.Lon,task.Lat}
	//做好冗余数据
	//find.SrcData = task.Scan("",5,"")
	find.SrcData = task.ToArray("")
	find.SrcData["id"] = task.Id

	e:=find.Save()
	if e != nil {println("任务位置更新时发生错误",e.Error())}
}
